Use PIMS to load video and sequential images

pims provides three classes for loading video.

  • ImageSequence reads images from a directory.
  • Video reads standard video files (AVI, MOV, etc.).
  • TiffStack reads multi-frame TIF / TIFF files.

Once loaded, these objects can be handled alike. In software terms, each is a subclass of a generic Frames object. The differences between the formats are handled quietly by pims.

Load sequential images from a directory.

Take ImageSequence as an example. We have a folder of images here:


In [1]:
ls image_sequence


T76S3F00001.png  T76S3F00002.png  T76S3F00003.png  T76S3F00004.png  T76S3F00005.png

We can load them into an ImageSequence object.


In [2]:
import pims

In [3]:
v = pims.ImageSequence('image_sequence/*.png')

We can see basic properties.


In [4]:
v


Out[4]:
<Frames>
Source: /home/tdimiduk/code/pims/examples/image_sequence/*.png
Length: 5 frames
Frame Shape: 424 x 640
Pixel Datatype: float32

We can print the first frame (it's an array of brightness values) or view those values as an image.


In [6]:
print v[0]


[[ 0.47450981  0.47843137  0.47843137 ...,  0.47058824  0.46666667
   0.47450981]
 [ 0.4627451   0.46666667  0.4627451  ...,  0.47058824  0.47450981
   0.47843137]
 [ 0.47058824  0.47843137  0.4627451  ...,  0.47450981  0.48235294
   0.49019608]
 ..., 
 [ 0.49019608  0.49411765  0.49019608 ...,  0.47843137  0.47843137
   0.47843137]
 [ 0.49411765  0.49803922  0.49411765 ...,  0.48627451  0.49019608
   0.49019608]
 [ 0.49411765  0.49803922  0.49411765 ...,  0.49411765  0.49019608
   0.48627451]]

In [7]:
%matplotlib inline
v[0]


Out[7]:

Use subsections of the loaded frames.

We can select a subset of the frames for viewing or processing. Examples:

  • v[3] frame three (an array)
  • v[:10] first 10 frames (a list of arrays)
  • v[2:5] frames 2-5 including 2 and 5 (a list of arrays)
  • v[100:] frames 100 to the end (a list of arrays)

Loop through the frames to do your image processing.


In [ ]:
for frame in v[:]:
    # Do something with frame, a numpy array.

Load video files or multi-frame TIFFs.

ImageSequence relies only on numpy and scipy, which are required dependencies of mr, so it works out of the box. Video needs OpenCV, which includes the Python module cv2. TiffStack needs libtiff.

Once these dependencies are in place, Video and TiffStack work in the same way as ImageSequence.


In [14]:
v = pims.Video('/home/dallan/mr/mr/tests/water/bulk-water.mov')
# This file is not included in PIMS, to keep the file size small.
# Try it with a video file of your own.

In [15]:
v


Out[15]:
<Frames>
Source File: /home/dallan/mr/mr/tests/water/bulk-water.mov
Frame Dimensions: 640 x 424
Cursor at Frame 0 of 480

In [8]:
v = pims.TiffStack('tiff_stack.tif')

In [9]:
v


Out[9]:
<Frames>
Source: tiff_stack.tif
Length: 5 frames
Frame Shape: 512 x 512
Pixel Datatype: <type 'numpy.uint16'>